package com.hava.spring_data;

import com.hava.spring_data.entity.Account;
import com.hava.spring_data.entity.User;
import com.hava.spring_data.repository.AccountJdbcTemplateDao;
import com.hava.spring_data.repository.MyJdbcTemplateDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Created by yanfa on 2016/10/26.
 */
public class AccountJdbcMain {

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("transaction-context.xml");

        AccountJdbcTemplateDao accountJdbcTemplateDao = context.getBean("accountJdbcTemplateDao", AccountJdbcTemplateDao.class);

        accountJdbcTemplateDao.resetMoney();
        List<Account> userList = accountJdbcTemplateDao.accountList();

        for(Account account : userList)
        {
            System.out.println(account.getUser() + ": " + account.getBalance());
        }

        try {
            accountJdbcTemplateDao.transferMoney("LiLei", "HanMeimei", 100);
        }catch (Exception e){
            System.out.println(e.toString());
        }

        for(Account account : accountJdbcTemplateDao.accountList())
        {
            System.out.println(account.getUser() + ": " + account.getBalance());
        }
        ((ConfigurableApplicationContext) context).close();
    }
}
